package com.arqui.admmon_module.client.core.load;

import java.util.Date;
import java.util.List;
import com.gwtplatform.dispatch.shared.DispatchAsync;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.ProxyCodeSplit;
import com.gwtplatform.mvp.client.annotations.NameToken;
import com.arqui.admmon_module.client.place.NameTokens;
import com.gwtplatform.mvp.client.proxy.ProxyPlace;
import com.google.inject.Inject;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.datepicker.client.DatePicker;
import com.gwtplatform.mvp.client.proxy.RevealContentEvent;
import com.arqui.admmon_module.client.action.HistoricLoad;
import com.arqui.admmon_module.client.action.HistoricLoadResult;
import com.arqui.admmon_module.client.core.load.LoadPresenter;
import com.arqui.admmon_module.shared.HistoricLoadEntity;

public class HistoricLoadPresenter extends
		Presenter<HistoricLoadPresenter.MyView, HistoricLoadPresenter.MyProxy> {

	public interface MyView extends View {
		public DatePicker getStartDP();
		public DatePicker getEndDP();
		public Button getConsultB();
		public FlexTable getHistoricT();
		public void setHistoricT(FlexTable historicT);
	}

	@ProxyCodeSplit
	@NameToken(NameTokens.historicload)
	public interface MyProxy extends ProxyPlace<HistoricLoadPresenter> {
	}

	@Inject
	public HistoricLoadPresenter(final EventBus eventBus, final MyView view,
			final MyProxy proxy) {
		super(eventBus, view, proxy);
	}

	@Override
	protected void revealInParent() {
		RevealContentEvent.fire(this, LoadPresenter.SLOT_content, this);
	}
	@Inject DispatchAsync dispatchAsync;

	@Override
	protected void onBind() {
		super.onBind();
		FlexTable historicoT= new FlexTable();
		historicoT= getView().getHistoricT();
		historicoT.setText(0, 0, "ID del archivo");
		historicoT.setText(0, 1, "Almacen");
		historicoT.setText(0, 2, "Sucursal");
		historicoT.setText(0, 3, "Fecha y hora de inicio");
		historicoT.setText(0, 4, "Fecha y hora de finalizaci\u00F3n");
		historicoT.setText(0, 5, "Estado");
		historicoT.setSize("957px", "27px");
		
		getView().getConsultB().addClickHandler(new ClickHandler() {
					
			@Override
			public void onClick(ClickEvent event){
				Date start=getView().getStartDP().getValue();
				Date end=getView().getEndDP().getValue();
				HistoricLoad action = new HistoricLoad(start, end);
				dispatchAsync.execute(action, getHistoricLoad );
			}
			});	
	}
	
	private final AsyncCallback<HistoricLoadResult> getHistoricLoad = new AsyncCallback<HistoricLoadResult>() {
		
		@Override
		public void onSuccess(HistoricLoadResult result) {
			List<HistoricLoadEntity> events= result.getResult();
			for(int i=1;i<events.size();i++){
				getView().getHistoricT().setHTML(i, 0, events.get(i).getId());
				getView().getHistoricT().setHTML(i, 1, events.get(i).getStartDate());
				getView().getHistoricT().setHTML(i, 2, events.get(i).getEndDate());
				getView().getHistoricT().setHTML(i, 3, events.get(i).getStore());
				getView().getHistoricT().setHTML(i, 4, events.get(i).getBranch());
				getView().getHistoricT().setHTML(i, 5, events.get(i).getState());
			}
			
			
			
		}
		
		@Override
		public void onFailure(Throwable caught) {				
		}
	};
}